import logging
from flask import jsonify

logger = logging.getLogger(__name__)

class APIResponse:
    """统一API响应工具类"""
    
    @staticmethod
    def success(data=None, message="操作成功"):
        """成功响应"""
        return jsonify({
            "status": "success",
            "message": message,
            "data": data
        })
    
    @staticmethod
    def error(message="操作失败", details=None):
        """错误响应"""
        logger.error(f"API错误: {message}, 详情: {details}")
        return jsonify({
            "status": "error",
            "message": message,
            "data": None
        })
    
    @staticmethod
    def check_error_result(result, error_message="操作失败"):
        """检查结果是否包含错误，如果有错误则返回错误响应"""
        if isinstance(result, dict) and "错误" in result:
            return APIResponse.error(f"{error_message}: {result['错误']}")
        return None 